using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;
using jaysJourney_v01.Common;


namespace jaysJourney_v01.Utils
{
    public class FrameAnimator
    {
        /**
         * MEMBER VARIABLES
         * */

        // Common

        private SpriteBatch parentSpriteBatch;
        private Texture2D[] frames;
        private String framename;


        private bool centerHor;

        // Animation

        private float fps;
        private bool isRunning = false;
        private bool isLooping;
        private int curFrame = 0;
        private int totalFrames;
        private double curTime = 0;

        private bool isFinished = false;
        private bool fadeout;
        private float alpha = 1;
        /**
         * METHODS
         * */

        // Constructor

        public FrameAnimator(SpriteBatch parentSpriteBatch, String framename, float fps, int totalFrames, bool isLooping, bool fadeout, JaysJourneyMain game)
        {
            this.parentSpriteBatch = parentSpriteBatch;
            this.frames = new Texture2D[totalFrames];
            this.framename = framename;
            this.fps = fps;
            this.totalFrames = totalFrames;
            this.isLooping = isLooping;
            this.fadeout = fadeout;
            initFrames(game);
        }

        private void initFrames(JaysJourneyMain game)
        {
            ContentManager Content = game.Content;
            for (int i = 0; i < this.frames.Length; i++)
            {
                if(i>9)this.frames[i] = Content.Load<Texture2D>(@"Textures\Frames\"+framename+@"\"+framename+"_"+i);
                else this.frames[i] = Content.Load<Texture2D>(@"Textures\Frames\" + framename + @"\" + framename + "_0" + i);
            }
        }

        // Play Animation

        public void animate(GameTime gameTime, Vector2 position)
        {
            if (isRunning)
            {
                this.curTime += (double)gameTime.ElapsedGameTime.Milliseconds / 1000d; // add gameTime

                if(this.curTime >= (1.0d / (double)fps)) // frame math
                {
                    this.curTime = 0; // reset curTime
                    this.curFrame++; // add frame

                    // loop or let last frame
                    if (this.curFrame >= this.totalFrames && isLooping) this.curFrame = 0;
                    else if (this.curFrame >= this.totalFrames && !isLooping)
                    {
                        this.curFrame = this.totalFrames - 1;
                        this.isFinished = true;
                        alpha -= (1.0f / fps);
                        if (alpha <= 0) this.stop();
                    }
                }
                this.parentSpriteBatch.Draw(this.frames[curFrame], position, Color.White*this.alpha);
                // Draw
            } 
        }

        // Start Animation

        public void play()
        {
            this.isRunning = true;
            this.alpha = 1;
        }

        // Stop Animation

        public void stop()
        {
            this.isRunning = false;
            this.curFrame = 0;
            this.isFinished = false;
        }

        // Pause Animation

        public void pause()
        {
            this.isRunning = false;
        }

        // Reset Animation

        public void reset()
        {
            this.curFrame = 0;
            this.isFinished = false;
        }

        // check if finished

        public bool hasFinished()
        {
            if (this.alpha > 0) return false;
            return true;
        }
    }
}
